<?php
//error_reporting(1);

function insert_attachment($file_handler, $post_id, $setthumb='false') {
		require_once(ABSPATH . "wp-admin" . '/includes/image.php');
		require_once(ABSPATH . "wp-admin" . '/includes/file.php');
		require_once(ABSPATH . "wp-admin" . '/includes/media.php');

		$attach_id = media_handle_upload( $file_handler, $post_id );

		if ($setthumb) update_post_meta($post_id,'_thumbnail_id',$attach_id);
		return $attach_id;
}

if(isset($_POST['post'])){
//	print "<pre>";
//	print_r($_POST);
//	print_r($_FILES);
//	print "</pre>";
//	exit;
	
	//////// adding post data
	$my_post = array(
		 'post_title' => $_POST['cname'],
		 'post_content' =>  $_POST['detail'],
		 'post_status' => 'publish',
		 'post_author' => 1,
		 'post_category' => array($_POST['subCat'], 109)
	  );
	 $pID=wp_insert_post( $my_post );	
	  
	///////// adding meta Information
	add_post_meta($pID, 'emailAddress', $_POST['email']);
	add_post_meta($pID, 'phoneNumber', $_POST['phone']);
	add_post_meta($pID, 'contactPerson', $_POST['cPerson']);
	add_post_meta($pID, 'siteURL', $_POST['siteUrl']);
	add_post_meta($pID, 'youTubeVideo', $_POST['video']);
	add_post_meta($pID, 'address', $_POST['address']);

	///////////// adding attachment
	if($_FILES['logo']['error']==0){
		insert_attachment('logo', $pID, true);
	}
	
	echo "All Done";
	exit;
}
?>

<script language="javascript" type="text/javascript">
function validateNewCompnay(){
	if(jQuery('#cname').val()==""){
		alert("Please enter company name");
		jQuery('#cname').focus();
		return false;
	}

	if(jQuery('#detail').val()==""){
		alert("Please enter company detail");
		jQuery('#detail').focus();
		return false;
	}

	if(!validateEmail(jQuery("#email").val())){
		alert("Please enter Comany Email Address");
		jQuery("#email").focus();
		return false;
	}

	if(jQuery("#phone").val()==""){
		alert("Please enter Company Phone Number");
		jQuery("#phone").focus();
		return false;
	}

	if(jQuery('#cPerson').val()==""){
		alert("Please enter company Contact Person Name");
		jQuery('#cPerson').focus();
		return false;
	}


	if(!is_valid_url(jQuery("#siteUrl").val())){
		alert("Please enter Comany Website Address");
		jQuery("#siteUrl").focus();
		return false;
	}

	return true;
}

function is_valid_url(url)
{
     return url.match(/^(ht|f)tps?:\/\/[a-z0-9-\.]+\.[a-z]{2,4}\/?([^\s<>\#%"\,\{\}\\|\\\^\[\]`]+)?$/);
}

function validateEmail(elementValue){  
   var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;  
   return emailPattern.test(elementValue);  
 }  


</script>

<div class="custom-form">

<form action="http://stoneselector.com/submit-your-site/" method="post" enctype="multipart/form-data">
<ul>
	<li>
    <span class="span">Company Name:</span>
	<input type="text" placeholder="Company Name" class="field-text" name="cname" id="cname" />
    </li>

	<li>
    <span class="span">Category:</span>
	<?php
		$args=array("parent"		=> 12,
					"hide_empty"	=> 0);
		$categories = get_categories($args); 
		
	?>
	<select class="styled" name="subCat">
    <?php foreach($categories as $category): ?>
	  	<option value="<?php echo $category->cat_ID ?>"><?php echo $category->name ?></option>
    <?php endforeach; ?>
	
    </select>
    </li>

	<li>
    <span class="span">Detail:</span>
	<textarea cols="30" rows="10" class="field-text" name="detail" id="detail"></textarea>
    </li>

	<li>
    <span class="span">Address:</span>
	<textarea cols="30" rows="10" class="field-text" name="address" id="address"></textarea>
    </li>

	<li>
    <span class="span">Email:</span>
	<input type="text" placeholder="Email" class="field-text" name="email" id="email" />
    </li>

	<li>
    <span class="span">Phone:</span>
	<input type="text" placeholder="Phone Number" class="field-text" name="phone" />
    </li>

	<li>
    <span class="span">Contact Person:</span>
	<input type="text" placeholder="Contact Person" class="field-text" name="cPerson" id="cPerson" />
    </li>

	<li>
    <span class="span">Site URL:</span>
	<input type="text" placeholder="Site URL" class="field-text" name="siteUrl" id="siteUrl" />
    </li>
	<li>
    <span class="span">Video:</span>
	<input type="text" placeholder="Youtube Video" class="field-text" name="video" id="video" />
    </li>
	<li>
    <span class="span">Company Logo:</span>
	<input type="file" placeholder="Logo" class="field-text" name="logo" />
    </li>


	<li class="form-button">
	<input type="submit" class="field-button" name="post" onclick="return validateNewCompnay()" />
    </li>
</ul>

</form>

</div>